/*
 * Created by JFormDesigner on Sun Mar 27 18:39:39 CST 2022
 */

package com.gxu.sjjg;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

/**
 * @author unknown
 */
public class Coursechoose extends JFrame {
    public Coursechoose() {
        initComponents();
    }
    // index表示对哪门成绩进行分析，实现函数复用 。ana是一个界面对象。
    public void solve(subGradeAnalyse ana, int index) throws IOException {
            BufferedReader file= new BufferedReader(new FileReader(new File("src/file/Grade.txt")));
            String s;
            int cout=0,left=index*10,right=left+9,highest=0,low =100,
                    grade90_100 = 0,grade80_89 = 0,grade70_79 = 0,grade60_69 = 0,failed = 0;//记录各分数段的人数。
            double average = 0;
            file.readLine();
            while ((s=file.readLine())!=null){
                cout++;
                int grade1=Integer.parseInt(s.substring(left,right).trim());
                highest=highest>grade1?highest:grade1;//更新最高分
                low=low<grade1?low:grade1;//更新最低分
                average=average+grade1;
                if(grade1>89)
                    grade90_100++;
                else if(grade1>79)
                    grade80_89++;
                else if(grade1>69)
                    grade70_79++;
                else if(grade1>59)
                    grade60_69++;
                else
                    failed++;
            }
            ana.hignest.setText(""+highest);
            if(cout==0){//如果还没有数据，不能被0除
                ana.average.setText("0");
                ana.low.setText("0");
            }
            else{
                int len=(""+average/cout).length();
                if(len>5) len=5;
                ana.average.setText((""+average/cout).substring(0,len));//不能直接（0，5）因为有可能没有五位
                ana.low.setText(""+low);
            }
            ana.grade90_100.setText(""+grade90_100);
            ana.grade80_89 .setText(""+grade80_89);
            ana.grade70_79.setText(""+grade70_79);
            ana.grade60_69.setText(""+grade60_69);
            ana.failed.setText(""+failed);

    }

    private void chinese(ActionEvent e) throws IOException {
        solve(new subGradeAnalyse(),2);
    }
    private void MathAnalyse (ActionEvent e) throws IOException {
        solve(new subGradeAnalyse(),3);
    }
    private void EnglishAnalyse(ActionEvent e) throws IOException {
        solve(new subGradeAnalyse(),4);
    }
    private void SportAnalyse(ActionEvent e) throws IOException {
        solve(new subGradeAnalyse(),5);
    }
    //界面代码
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        label1 = new JLabel();

        //======== this ========
        setTitle("\u9009\u62e9\u8bfe\u7a0b");
        setVisible(true);
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- button1 ----
        button1.setText("\u8bed\u6587");
        button1.addActionListener(e -> {
            try {
                chinese(e);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(90, 20), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u6570\u5b66");
        button2.addActionListener(e -> {
            try {
                MathAnalyse(e);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(90, 65), button2.getPreferredSize()));

        //---- button3 ----
        button3.setText("\u82f1\u8bed");
        button3.addActionListener(e -> {
            try {
                EnglishAnalyse(e);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        contentPane.add(button3);
        button3.setBounds(new Rectangle(new Point(90, 110), button3.getPreferredSize()));

        //---- button4 ----
        button4.setText("\u4f53\u80b2");
        button4.addActionListener(e -> {
            try {
                SportAnalyse(e);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        contentPane.add(button4);
        button4.setBounds(new Rectangle(new Point(90, 155), button4.getPreferredSize()));

        //---- label1 ----
        label1.setIcon(new ImageIcon(getClass().getResource("/myimage/loginimg.jpg")));
        contentPane.add(label1);
        label1.setBounds(0, 0, 230, 220);

        contentPane.setPreferredSize(new Dimension(230, 250));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
